//
//  CoursesClassViewController.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/10/20.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//  课程 

#import "CoursesClassViewController.h"
#import "CoursesClassTableViewCell.h"
#import "CourseClassInfoViewController.h"
#import "CourseModel.h"
#import "NewAddCommonTeachViewController.h"
#import "WebAlertView.h"
#import "CourseClassAlertView.h"
//#import "AddCommonClassViewController.h"
//#import "AddPrivateClassViewController.h"


@interface CoursesClassViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,weak)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@property(nonatomic,assign)BOOL isFirst;

@property(nonatomic,strong)NoDataView *noDataView;

@property(nonatomic,weak)CourseClassAlertView *courseClassView;

@end

@implementation CoursesClassViewController

-(void)loadServerData{
    
    if (!self.isFirst) {
        
        [self loadData];
    }
    
    if (self.dataSource.count == 0) {
        
        self.noDataView = [NoDataView shareInstance];
        
        [self.noDataView showInView:self.view];
    }
}

- (void)viewDidLoad {
    
    [super viewDidLoad];

    self.view.backgroundColor = BackGroundColor;
    
    [self createSubView];
    
    [NotificationCenter addObserver:self selector:@selector(loadData) name:@"ReloadCourse" object:nil];
    
}

#pragma mark 创建子视图
-(void)createSubView{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.estimatedRowHeight = 200;
    [tableView registerClass:[CoursesClassTableViewCell class] forCellReuseIdentifier:@"CoursesClassTableViewCell"];
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    tableView.backgroundColor = CSClearColor;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    [tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view).offset(20);
        make.right.mas_equalTo(self.view).offset(-20);
        if (iPhoneX) {
        
            make.bottom.mas_equalTo(self.view).offset(-NavagationBarHeightIphoneX - 60);
        }
        else{
            
            make.bottom.mas_equalTo(self.view).offset(-NavagationBarHeight1 - 60);
        }
    }];
}


//获取数据
-(void)loadData{
    
    self.isFirst = YES;
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [params setObject:self.brandModel.brandID forKey:@"brandid"];
    
    if ([self.cardType isEqualToString:@"mord"]) {
        
        [params setObject:@"团课" forKey:@"type"];
    }
    else{
        
        [params setObject:@"私教" forKey:@"type"];
    }
    
    [HTTPTool GET:Get_Course_List parameters:params
     
              success:^(id responseObject){
                  
                  [MBProgressHUD hideHUDForView:self.view];
                  
                  if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                      
                      self.dataSource = [NSMutableArray arrayWithArray:[responseObject objectForKey:@"CourseList"]];
                      
                      self.noDataView = [NoDataView shareInstance];
                      
                      [self.noDataView hideInView:self.view];
                      
                      if (self.dataSource.count == 0) {
                          
                          self.noDataView = [NoDataView shareInstance];
                          
                          [self.noDataView showInView:self.view];
                          
                          [self showNoticeView];
                      }
                      
                      [self.tableView reloadData];
                  }
              }
     
              failure:^(NSError *error){
                  
                  [MBProgressHUD hideHUDForView:self.view];
              }];
    
    
}


-(void)showNoticeView{
    
    CourseClassAlertView *courseClassView = [[CourseClassAlertView alloc] init];
    
    if ([self.cardType isEqualToString:@"mord"]) {
        
        courseClassView.titleLabel.text = @"添加团课";
        
        courseClassView.noticeLabel.text = @"暂无团课";
        
        courseClassView.noticeLabel1.text = @"前往添加团课";
        
        [courseClassView.addBtn setTitle:@"添加团课"];
    }
    else{
        
        courseClassView.titleLabel.text = @"添加私教";
        
        courseClassView.noticeLabel.text = @"暂无私教";
        
        courseClassView.noticeLabel1.text = @"前往添加私教";
        
        [courseClassView.addBtn setTitle:@"添加私教"];
    }
    
    [courseClassView.addBtn addTarget:self action:@selector(addCourseClass)];
    
    [[UIApplication sharedApplication].keyWindow addSubview:courseClassView];
    
    self.courseClassView = courseClassView;
}

#pragma mark 添加课程种类
-(void)addCourseClass{
    
    [self.courseClassView removeFromSuperview];
    
//    if ([self.cardType isEqualToString:@"mord"]) {
//        
//        AddCommonClassViewController *addCommonClassVC = [[AddCommonClassViewController alloc] init];
//        
//        addCommonClassVC.brandModel = self.brandModel;
//        
//        [self.navigationController pushViewController:addCommonClassVC animated:YES];
//    }
//    else{
//        
//        AddPrivateClassViewController *addPrivateClassVC = [[AddPrivateClassViewController alloc] init];
//        
//        addPrivateClassVC.brandModel = self.brandModel;
//        
//        [self.navigationController pushViewController:addPrivateClassVC animated:YES];
//    }
    
}

#pragma mark UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

#pragma mark UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 8;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return nil;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CoursesClassTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CoursesClassTableViewCell"];
    
    NSDictionary *courseDict = [self.dataSource objectAtIndex:indexPath.section];
    
    CourseModel *courseModel = [CourseModel mj_objectWithKeyValues:courseDict];
    
    cell.courseModel = courseModel;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.vcFrom isEqualToString:@"teach"]) {
        
        NSDictionary *courseDict = [self.dataSource objectAtIndex:indexPath.section];
        
        CourseModel *courseModel = [CourseModel mj_objectWithKeyValues:courseDict];
        
        NewAddCommonTeachViewController *newAddCommonTeachVC = [[NewAddCommonTeachViewController alloc] init];
        
        newAddCommonTeachVC.courseModel = courseModel;
        
        newAddCommonTeachVC.teachDict = self.teachDict;
        
        newAddCommonTeachVC.type = self.cardType;
        
        [self.navigationController pushViewController:newAddCommonTeachVC animated:YES];
    }
    else{
        
        NSDictionary *courseDict = [self.dataSource objectAtIndex:indexPath.section];

        CourseModel *courseModel = [CourseModel mj_objectWithKeyValues:courseDict];

        CourseClassInfoViewController *courseClassInfoVC = [[CourseClassInfoViewController alloc] init];

        courseClassInfoVC.courseModel = courseModel;

        [self.navigationController pushViewController:courseClassInfoVC animated:YES];
    }
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.vcFrom isEqualToString:@"teach"]){
        
        return NO;
    }
    else{
        
        return NO;
    }
}

//-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
//
//    return @"删除";
//}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

/**
 *  左滑cell时出现什么按钮
 */
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    __weak typeof(self)weakSelf = self;
    
    NSDictionary *courseDict = [self.dataSource objectAtIndex:indexPath.section];
    
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
                
        [weakSelf deleteCourseClassWithId:[NSString stringWithFormat:@"%@",[courseDict objectForKey:@"ID"]]];
        
        tableView.editing = NO;
    }];
    
    return @[action1];
}


/**
 删除课程
 */
-(void)deleteCourseClassWithId:(NSString *)courseId{
    
}

-(void)dealloc{
        
    [NotificationCenter removeObserver:self];
}

#pragma mark Get
-(NSMutableArray *)dataSource{
    
    if (!_dataSource) {
        
        _dataSource = [[NSMutableArray alloc] init];
        
    }
    
    return _dataSource;
}

@end
